import unittest
from navi_func import *
import time
from map_func import *
from remote_api import *
from pprint import pprint

robot =r'Car-Robot\data\robot_loc.json'

class NaviTestCase(unittest.TestCase):
    '''测试 navi_function.py'''
    
    
        
    def test_dijsktra(self):
        '''测试 dijsktra 函数'''
        map = MapFunction()
        navi = NaviFunction()
        shortest_path = navi.dijkstra_shortest_path(1235431,1235429,map.graph)
        self.assertEqual(shortest_path, [1235431, 1235430,1235429])
        test_path_1 = navi.dijkstra_shortest_path(1235159,1235237,map.graph)
        self.assertEqual(isinstance(test_path_1, list), True)

        test_path_2 = navi.dijkstra_shortest_path(1235159,1235416,map.graph)

        self.assertEqual(isinstance(test_path_2, list), True)

        test_path_3 = navi.dijkstra_shortest_path(1234889,1235231,map.graph)
        self.assertEqual(isinstance(test_path_3, list), True)
        test_path_4 = navi.dijkstra_shortest_path(1234890,1235324,map.graph)
        test_path_5 = navi.dijkstra_shortest_path(1234888,1235347,map.graph)
        print(test_path_5)
        # print(shortest_path)
        # pprint(graph)
    
    def test_location(self,file_path = robot):
        '''测试 location 函数'''
        navi = NaviFunction()
        map = MapFunction()
        robot_loc = map.robot_location(file_path)
        # self.assertEqual(robot_loc, [1235159, 1235160, 1234889, 1234890, 1234888])
        # print(robot_loc)

    def test_multi_dijkstra(self,file_path = robot):
        '''测试 multi_dijkstra 函数'''
        navi = NaviFunction()
        map = MapFunction()
        robot_loc = map.robot_location(file_path)
        dst_loc = [1235237, 1235416, 1235231, 1235324, 1235347]
        shortest_path_list = navi.multi_dikstra_shortest_path(robot_loc, dst_loc, map.graph)
        # pprint(shortest_path_list)
    
    def test_multi_navi(self):
        navi = NaviFunction()
        map = MapFunction()
        api = RemoteAPI()
        api.status()
        loc = api.get_json_parse()
        pprint(loc)
        robot_loc = map.robot_loc_by_dic(loc)
        dst_loc = [1235237, 1235416, 1235231, 1235324, 1235347]
        shortest_path_list = navi.multi_dikstra_shortest_path(robot_loc, dst_loc, map.graph)
        robot_id = 106
        commands = []
        for path in shortest_path_list:
            for next in shortest_path_list[path]:
                payload = api.post_json_gen(robot_id,map.point_dic[next])
                commands.append(payload)
                api.control(payload)
                time.sleep(0.5)
            robot_id += 1  
        pprint(shortest_path_list)
        pprint(commands)
    
    def test_fetch_stuff(self):
        navi = NaviFunction()
        map = MapFunction()
        api = RemoteAPI()
        api.status()
        dst_loc = [1235424, 1235413, 1235417, 1235428, 1235326] #这是取货点坐标id
        loc = api.get_json_parse()
        robot_loc = map.robot_loc_by_dic(loc)
        shortest_path_list = navi.multi_dikstra_shortest_path(robot_loc, dst_loc, map.graph)
        robot_id = 106
        commands = []
        for path in shortest_path_list:
            for next in shortest_path_list[path]:
                payload = api.post_json_gen(robot_id,map.point_dic[next])
                commands.append(payload)
                api.control(payload)
                time.sleep(0.5)
            robot_id += 1  
        pprint(shortest_path_list)
        pprint(commands)
    
    


if __name__ == '__main__':
    unittest.main()
    
